*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		3d_tables_ancova_subwellbe.do
*-------------------------------------------------------------------------------
* Description		Creates subjective well-being ANCOVA table
* Outputs		Table A17 
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Subjective well being table
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================
clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data
global results "$path/results"		
global inputPath "$path/datasets"
cd "$results"									// Change to directory to store results
set more off
set trace off
*Loading processed data
	use $inputPath/impactDatabase_wide.dta, clear		

*Merge the asesor data here. 															
merge 1:1 idBaseSurvey using $inputPath/asesor2008.dta
drop _merge 

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* Section 2.1 - Table on Subjective well-being 
*-------------------------------------------------------------------------------
* 2.1.1.Calculates table content -----------------------------------------------
*-------------------------------------------------------------------------------

	eststo clear
	local i = 1	

	foreach y in anxietyFuture economicDifficulty { 
		
		gen nonmiss = 1 if `y'2009 !=. & `y'2008 !=. //Marking subjects nonmissing in both periods		
		reghdfe `y'2009 i.accountAccess `y'2008  if nonmiss ==1,    	///
			abs(asesor2008) vce(cluster groupId) 

		
	//Adding control mean		
		sum `y'2009 if accountAccess == 0
		if (abs(`r(mean)') < 100) {
			estadd local control_mean = trim("`: display %12.3fc r(mean)'")
		} 
		else {
			estadd local control_mean = trim("`: display %12.0fc r(mean)'")
		}
		
	//AES calculation, step 1: normalizes variables
		sum `y'2009 if accountAccess==0 //computes sd
		gen aes_cc`i'2009 = `y'2009 / r(sd) //normalizes outcome variable
		
		sum `y'2008 if accountAccess==0 //computes sd
		gen aes_cc`i'2008 = `y'2008 / r(sd) //normalizes outcome variable
		
		estadd local ind_fixeff = "No"
		estadd local str_fixeff = "Yes"

		drop nonmiss
		
		eststo
		local i = `i' + 1 //updates counter	
		
	}
	
//AES calculation, step 2: reshapes the data to implement regression
// we keep only variables required
	keep aes_cc* idBaseSurvey economicShock2009 groupId accountAccess asesor2008
	reshape long aes_cc, i(idBaseSurvey) j(out)
	
	tostring(out), generate(year)
	replace year = substr(year, 2, 4)
	destring(year), replace //creates year variable 
	
	tostring(out), replace
	replace(out) = substr(out, 1, 1)
	destring(out), replace //creates out variable
	
	reshape wide aes_cc , i(idBaseSurvey out) j(year)
	
//AES: Implements regression
	reghdfe aes_cc2009 c.accountAccess#i.out ///
	i.out aes_cc2008, abs(asesor2008) vce(cluster groupId)

//AES: computes AES
	lincom((1.out#c.accountAccess + 2.out#c.accountAccess)/2)	

	local aes_se = trim("`: display %12.3fc r(se)'")
	local aes_p = 2*ttail(`r(df)',abs(`r(estimate)' / `r(se)'))

// stores stars for coefficient
	if (`aes_p' <= 0.1 & `aes_p' > 0.05 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'*")
	}
	else if (`aes_p' <= 0.05 & `aes_p' > 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'**")
	}
	else if (`aes_p' <= 0.01 ) { 
		local aes_b = trim("`: display %12.3fc r(estimate)'***")
	}
	else {
		local aes_b = trim("`: display %12.3fc r(estimate)'")
	}

*-------------------------------------------------------------------------------
* 2.1.2.Calculates table content ---------------------------------------------
*-------------------------------------------------------------------------------

	esttab est* using tab_subwellbe_ancova.tex, replace nonumber 					///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber			///
	stats(control_mean N ind_fixeff str_fixeff, fmt(%12.3fc %12.0fc %12.0fc)			///
	label("Control mean" "\hline Observations"							///
	"Individual FE" "Stratum FE")) mlabels(,none) 							///	
	collabels(, none) eqlabels(, none)  								///
	keep(1.accountAccess)										///
	varlabels(1.accountAccess "Account") 								///
	starlevels(* 0.1 ** 0.05 *** 0.01) 								///
	prehead(\begin{tabular}{l*{@M}{c}} \hline \hline 						///	
	&(1) & (2) \\ 											///	
	&Anxiety about & Recent economic \\ 								///
	& financial future & difficulty \\)  								///
	posthead(\hline ) prefoot( ) 									///
	postfoot(\hline \multicolumn{@span}{c}{\textbf{AES:} `aes_b' (`aes_se')} \\ 			///
	\hline \hline \end{tabular})
 	
